package com.kevin.swing.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lombok.Data;
import lombok.EqualsAndHashCode;

import com.kevin.MainFrame;
import com.kevin.util.ConfigProperties;

@EqualsAndHashCode(callSuper = false)
/**
 * 分词设置
 * @ClassName WordSetDialog
 * @author <a href="mailto:donggongai@126.com" target="_blank">kevin</a>
 * @date 2016年9月24日 上午10:44:39
 *
 */
public @Data class WordSetDialog extends JDialog {
    private MainFrame owner;

    /**
	 * 
	 */
    private static final long serialVersionUID = 6792501234970255123L;

    private JLabel wordSetLabel;
    private JTextArea wordSetTA;
    private JScrollPane wordSetJsp;

    private JPanel btnPanel = new JPanel();
    private JButton okbtn = new JButton("保存");
    private JButton clbtn = new JButton("取消");

    public WordSetDialog(MainFrame owner) {
        super();
        this.owner = owner;
        wordSetLabel = new JLabel("<html>您可以在此设置您的自定义分词，设定好之后生成实体类的过程中将按定义的词语进行驼峰命名。<br/>请以英文的逗号分割不同的词语。</html>");
        wordSetTA = new JTextArea();
        wordSetTA.setLineWrap(true);
        wordSetTA.setBorder(BorderFactory.createTitledBorder("分词"));
        wordSetTA.setText(ConfigProperties.getString(ConfigProperties.WORDSET, ""));

        wordSetJsp = new JScrollPane(wordSetTA);
        wordSetJsp.setBorder(null);
        okbtn = new JButton("确定");
        clbtn = new JButton("取消");

        btnPanel = new JPanel();
        btnPanel.add(okbtn);
        btnPanel.add(clbtn);

        setTitle("设定自定义分词");
        add(wordSetLabel, BorderLayout.NORTH);
        add(wordSetJsp, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.SOUTH);
        setVisible(false);
        setSize(400, 200);
        setResizable(false);
        init();
    }

    private void init() {
        clbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                wordSetTA.setText(ConfigProperties.getString(ConfigProperties.WORDSET, ""));
                WordSetDialog.this.setVisible(false);
            }
        });

        okbtn.addActionListener(e -> {
            String wordset = getWordSetTA().getText();
            ConfigProperties.setProperty(ConfigProperties.WORDSET, wordset);
            WordSetDialog.this.setVisible(false);
        });

    }

}
